<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "channel_visit".
 *
 * @property integer $id
 * @property string $ip
 * @property string $utm
 * @property integer $action
 * @property string $created_at
 * @property string $created_date
 */
class ChannelVisit extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'channel_visit';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ip', 'utm', 'action'], 'required'],
            [['action'], 'integer'],
            [['created_at', 'created_date'], 'safe'],
            [['ip'], 'string', 'max' => 64],
            [['utm'], 'string', 'max' => 36],
            ['utm', 'exist', 'targetClass' => 'app\models\Channel']//只有已添加的渠道才记录
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'ip' => '访问ip',
            'utm' => '渠道参数',
            'action' => '动作,1为打开首页,2为点击下载,3为从lonlife.cn跳转',
            'created_at' => '创建时间',
            'created_date' => '创建日期',
        ];
    }

    /**
     * 方法说明：根据该次访问的ｕｔｍ来查找对应的渠道信息
     *
     * 作者：张华杰 blues@lonlife.cn
     * 时间：2017-11-13 2:54 PM
     */
    public function getChannel()
    {
        return $this->hasOne(Channel::className(),['utm' => 'utm']);
    }

    public function beforeSave($insert)
    {
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }
}
